package io;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FileDemo5 {

  public static void main(String[] args) {

    FileDemo5 fileDemo5 = new FileDemo5();
    fileDemo5.inchina();


  }

 private class LonLat {
    double lon;
    double lat;

    public LonLat(double lon, double lat) {
      this.lon = lon;
      this.lat = lat;
    }

    @Override
    public String toString() {
      return "LonLat{" +
              "lon=" + lon +
              ", lat=" + lat +
              '}';
    }
  }


  public void inchina() {

    List<String> train;
    List<String> flightline = null;
    Map<String, LonLat> nameLonLat = Maps.newHashMap();
    try {
      train = FileUtils.readLines(new File("/Users/ycy/Desktop/yin/district_inchina.txt"), "UTF-8");
      for (String s : train) {
//        System.out.println(s);
        String[] split = s.split("\t");
        nameLonLat.put(split[0].trim(), new LonLat(Double.valueOf(split[1]), Double.valueOf(split[2])));
      }
    } catch (IOException e) {
      e.printStackTrace();
    }


    Set<String> notmatchdistrict = Sets.newHashSet();
    List<String> res = Lists.newLinkedList();
    try {
      flightline = FileUtils.readLines(new File("/Users/ycy/Desktop/yin/flightline_inchina.txt"), "UTF-8");
      for (String s : flightline) {
        //
        s = s.replaceAll("的机票", "");
        String[] split1 = s.split("到");

        String start = split1[0].trim();
        String arrival = split1[1].trim();

        LonLat lonLat = get(nameLonLat, start);
        if (lonLat == null) {
          notmatchdistrict.add(start);

        }

        LonLat lonLat1 = get(nameLonLat, arrival);
        if (lonLat1 == null) {
          notmatchdistrict.add(arrival);
        }
        if (lonLat != null && lonLat1 != null) {
//          double meter1 = SloppyMath.haversin(lonLat.lat, lonLat.lon, lonLat1.lat, lonLat1.lon);
//          GlobalCoordinates source = new GlobalCoordinates(lonLat.lat, lonLat.lon);
//          GlobalCoordinates target = new GlobalCoordinates(lonLat1.lat, lonLat1.lon);
//          double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
          double meter1 = getDistance(lonLat.lat, lonLat.lon, lonLat1.lat, lonLat1.lon);
//          String x = s + "," + (meter1 );
//          String x = s + "," + (meter1/1000 ) + lonLat + lonLat1;
          String x = s + "," + (meter1 / 1000);
          System.out.println(x);
         if(meter1 < 300000) res.add(x);
        } else {
          String x = s + "," + 0;
          System.out.println(x);
//          res.add(x);
        }

      }
      System.out.println(notmatchdistrict);

      //输出
      FileUtils.writeLines(new File("/Users/ycy/Desktop/yin/res_inchina.txt"), res);

    } catch (IOException e) {
      e.printStackTrace();
    }
  }


  private LonLat get(Map<String, LonLat> nameLonLat, String city) {
    LonLat lonLat = null;
    if (nameLonLat.containsKey(city)) {
      return nameLonLat.get(city);
    }
    if (nameLonLat.containsKey(city + "区")) {
      return nameLonLat.get(city + "区");
    }
    if (nameLonLat.containsKey(city + "市")) {
      return nameLonLat.get(city + "市");
    }
    if (nameLonLat.containsKey(city + "镇")) {
      return nameLonLat.get(city + "镇");
    }
    if (city.endsWith("市") && nameLonLat.containsKey(city.substring(0, city.length() - 1))) {
      return nameLonLat.get(city.substring(0, city.length() - 1));
    }

    return null;
  }



  public static double getDistance(double long1, double lat1, double long2,
                                   double lat2) {
    double a, b, R;
    R = 6378137; // 地球半径
    lat1 = lat1 * Math.PI / 180.0;
    lat2 = lat2 * Math.PI / 180.0;
    a = lat1 - lat2;
    b = (long1 - long2) * Math.PI / 180.0;
    double d;
    double sa2, sb2;
    sa2 = Math.sin(a / 2.0);
    sb2 = Math.sin(b / 2.0);
    d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1)
            * Math.cos(lat2) * sb2 * sb2));
    return d;
  }

  /**
   * 地球半径
   */
  private static double EarthRadius = 6378.137;

  /**
   * 经纬度转化成弧度
   * Add by 成长的小猪（Jason.Song） on 2017/11/01
   * http://blog.csdn.net/jasonsong2008
   *
   * @param d 经度/纬度
   * @return
   */
  private static double rad(double d) {
    return d * Math.PI / 180.0;
  }

}
